package PTA.练习04;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-11-15
 * Time: 21:23
 */

//实现一个栈类
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int maxSize = sc.nextInt();
        Stack stack = new Stack();
        stack.createStack(maxSize);
        for (int i = 0; i < maxSize; i++) {
            stack.push(sc.nextInt());
        }
        int PopTime = sc.nextInt();
        for (int i = 0; i < PopTime; i++) {
            System.out.println("取出栈顶元素");
            System.out.println(stack.pop());
        }
        int PushTime = sc.nextInt();
        for (int i = 0; i < PushTime; i++) {
            stack.push(sc.nextInt());
        }
        System.out.println("查看栈顶元素");
        stack.pull();
        stack.getCapacity();//查看个数
    }
}

class Stack{
    //创建一个容量为size的栈
    int size;
    int[] array;
    int c=0;
    public void createStack(int size){
        this.size=size;
        array=new int[size];
    }
    //判断栈是否已满
    public boolean isFull(){
        return c == size;
    }
    // 判断栈是否为空
    public boolean isEmpty(){
        return c == 0;
    }
    // 将一个值为data的元素入栈,如果栈满则输出栈满
    public void push(int data){
        if(isFull()){
            System.out.println("栈满");
            return;
        }
        array[c++]=data;
    }
    // 抛出栈顶元素，如果栈空则返回-1
    public int pop(){
        if (isEmpty()){
            return -1;
        }
        return array[--c];

    }
    // 打印栈顶元素，如果栈空则打印栈空
    public void pull(){
        if (isEmpty()){
            System.out.println("栈空");
            return;
        }
        System.out.println(array[c-1]);
    }
    //打印当前栈内元素个数
    public void getCapacity(){
        System.out.println(c);
    }
}

